﻿using Common.Communal;
using ERP.Domain.CustomerSupplier;
using ERP.Domain.SalesManagement;
using MediatR;
using Sell.Api.Read.Application.Command.ProcureCommand;
using Sell.DTO;
using SqlSugar;

namespace Sell.Api.Read.Application.CommandHandlers.ProcureCommandHanlders
{
    public class ProcurelistCommandHandlers : IRequestHandler<ProcurelistCommand, ApiResult<ApiPaging<procurelistdto>>>
    {
        public ISqlSugarClient SqlSugar { get; }
        public ProcurelistCommandHandlers(ISqlSugarClient sqlSugar)
        {
            SqlSugar = sqlSugar;
        }
        /// <summary>
        /// 采购单列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<ApiPaging<procurelistdto>>> Handle(ProcurelistCommand request, CancellationToken cancellationToken)
        {
            ApiResult<ApiPaging<procurelistdto>> res = new ApiResult<ApiPaging<procurelistdto>>();
            res.Code = EnumCode.Ok;
            res.Message = "采购单显示成功";
            /// 初始化查询：关联 Procure表和Contract表
            var query = SqlSugar.Queryable<Procure>()
                .LeftJoin<Supplier>((p, s) => p.SupplierId == s.Id)
                .LeftJoin<Contract>((p,s,c)=>p.ContractId==c.Id)
                .Where(p=> p.IsDelete == false).Select(
                (p,s,c) => new procurelistdto
                {
                    Id =p.Id,
                     ContractCode = c.ContractCode,
                      ContractId=p.ContractId,
                       ContractName=c.ContractName,
                        CreateTime=c.CreateTime,
                         IsDelete=p.IsDelete,
                          Payment=p.Payment,
                           ProcureCode=p.ProcureCode,
                            ProcureTime=p.ProcureTime,
                             SalesTax=p.SalesTax,
                              SupplierCode=s.SupplierCode,
                               SupplierId=p.SupplierId,
                                SupplierName=s.SupplierName,
                                 TaxRate=p.TaxRate,
                                  TaxRateType=p.TaxRateType,
                });
            if (!string.IsNullOrEmpty(request.ProcureCode))
            {
                query = query.Where(p => p.ProcureCode.Contains(request.ProcureCode));
            }
            if (request.SupplierId != null)
            {
                query = query.Where(p => p.SupplierId.Equals(request.SupplierId));
            }
            //SqlSugar分页查询实现
            ApiPaging<procurelistdto> result = new ApiPaging<procurelistdto>();
            result.TotalCount = query.Count();//查询总记录数
            result.TotalPage = (int)Math.Ceiling(result.TotalCount * 1.0 / request.pagesize);//计算总页数
            result.Data = query.Skip((request.pageindex - 1) * request.pagesize)//跳过前面的记录
                .Take(request.pagesize)//取指定页大小的记录
                .ToList();//执行查询并转为List
            res.Success(result, "查询成功");
            return res;
        }
    }
}
